package org.amphiprion.montrictracv4.swipe;

import java.util.ArrayList;
import java.util.List;

import org.amphiprion.montrictracv4.R;

import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.TextView;

import com.nineoldandroids.animation.Animator;
import com.nineoldandroids.animation.AnimatorListenerAdapter;
import com.nineoldandroids.view.ViewHelper;
import com.nineoldandroids.view.ViewPropertyAnimator;

public class UndoBarController {
	private ViewPropertyAnimator mBarAnimator;
	private View mBarView;
	private Handler mHideHandler = new Handler();
	private Runnable mHideRunnable = new Runnable() {
		@Override
		public void run() {
			UndoBarController.this.hideUndoBar(false);
			mUndoListener.onNotUndo(undoContents);
			undoContents.clear();
		}
	};
	private TextView mMessageView;
	private UndoListener mUndoListener;
	private CharSequence mUndoMessage;
	// private Object mUndoToken;
	// protected int mPosition;

	private List<UndoContent> undoContents = new ArrayList<UndoContent>();

	public UndoBarController(View paramView, UndoListener paramUndoListener) {
		this.mBarView = paramView;
		this.mBarAnimator = ViewPropertyAnimator.animate(this.mBarView);
		this.mUndoListener = paramUndoListener;
		this.mMessageView = ((TextView) this.mBarView.findViewById(R.id.undobar_message));
		this.mBarView.findViewById(R.id.undobar_button).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View paramAnonymousView) {
				UndoBarController.this.hideUndoBar(false);
				UndoBarController.this.mUndoListener.onUndo(undoContents);
				undoContents.clear();
			}
		});
		hideUndoBar(true);
	}

	public void hideUndoBar(boolean quick) {
		this.mHideHandler.removeCallbacks(this.mHideRunnable);
		if (quick) {
			this.mBarView.setVisibility(View.GONE);
			ViewHelper.setAlpha(this.mBarView, 0.0F);
			this.mUndoMessage = null;
			// this.mUndoToken = null;
		} else {
			this.mBarAnimator.cancel();
			this.mBarAnimator.alpha(0.0F).setDuration(300).setListener(new AnimatorListenerAdapter() {
				@Override
				public void onAnimationEnd(Animator paramAnonymousAnimator) {
					UndoBarController.this.mBarView.setVisibility(View.GONE);
					UndoBarController.this.mUndoMessage = null;
					// UndoBarController.this.mUndoToken = null;
				}
			});
		}

	}

	public void onRestoreInstanceState(Bundle paramBundle) {
		// if (paramBundle != null) {
		// this.mUndoMessage = paramBundle.getCharSequence("undo_message");
		// this.mUndoToken = paramBundle.getParcelable("undo_token");
		// if ((this.mUndoToken != null) || (!TextUtils.isEmpty(this.mUndoMessage))) {
		// showUndoBar(true, this.mUndoMessage, this.mUndoToken, this.mPosition);
		// }
		// }
	}

	public void showUndoBar(boolean quick, CharSequence paramCharSequence, Object paramObject, int paramInt) {
		// this.mUndoToken = paramObject;
		// this.mPosition = paramInt;
		undoContents.add(0, new UndoContent(paramObject, paramInt));
		this.mUndoMessage = paramCharSequence;
		this.mMessageView.setText(this.mUndoMessage);
		this.mHideHandler.removeCallbacks(this.mHideRunnable);
		this.mHideHandler.postDelayed(this.mHideRunnable, 5000L);
		this.mBarView.setVisibility(0);
		if (quick) {
			ViewHelper.setAlpha(this.mBarView, 1.0F);
		} else {
			this.mBarAnimator.cancel();
			this.mBarAnimator.alpha(1.0F).setDuration(300).setListener(null);
		}
	}

	public static abstract interface UndoListener {
		public abstract void onUndo(List<UndoContent> undoContents);

		public abstract void onNotUndo(List<UndoContent> undoContents);
	}

	public class UndoContent {
		public UndoContent(Object param, int position) {
			this.param = param;
			this.position = position;
		}

		public Object param;
		public int position;
	}
}